package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import zhihuilan.dto.MyOrderDto;
import zhihuilan.dto.MyOrderDto;
import zhihuilan.dto.WxMiniPageDto;
import zhihuilan.entity.MyOrder;
import zhihuilan.entity.MyOrder;
import zhihuilan.entity.MyOrderDetail;
import zhihuilan.mapper.MyFactoryFavMapper;
import zhihuilan.mapper.MyOrderDetailMapper;
import zhihuilan.mapper.MyOrderMapper;
import zhihuilan.params.query.MyOrderQueryParam;
import zhihuilan.service.MyOrderService;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 部门管理 服务实现
 *
 * @author ruoyi
 */
@Service
public class MyOrderServiceImpl extends ServiceImpl<MyOrderMapper, MyOrder> implements MyOrderService {

    @Override
    public WxMiniPageDto<MyOrderDto> selectPageList(MyOrderQueryParam queryParam) {
        QueryWrapper<MyOrder> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(queryParam)) {
            if (!ObjectUtils.isEmpty(queryParam.getStaffid())) {
                queryWrapper.eq("staffid", queryParam.getStaffid());
            }
            if (!ObjectUtils.isEmpty(queryParam.getShopid())) {
                queryWrapper.eq("shopid", queryParam.getShopid());
            }
            if (!ObjectUtils.isEmpty(queryParam.getUtype())) {
                queryWrapper.eq("utype", queryParam.getUtype());
            }
            if (!ObjectUtils.isEmpty(queryParam.getType())) {
                queryWrapper.eq("type", queryParam.getType());
            }
            if (!ObjectUtils.isEmpty(queryParam.getOrderno())) {
                queryWrapper.eq("orderno", queryParam.getOrderno());
            }
            if (!ObjectUtils.isEmpty(queryParam.getSourceType())) {
                queryWrapper.eq("source_type", queryParam.getSourceType());
            }
        } else {
            return null;
        }
        queryWrapper.orderByDesc("addtime");
        long pageNum = 0;
        long pageSize = 10;
        if (queryParam.getPage() >= 0L) {
            pageNum = queryParam.getPage();
        }
        if (queryParam.getPageSize() > 0L) {
            pageSize = queryParam.getPageSize();
        }
        Page<MyOrder> page = new Page<>(pageNum, pageSize);
        this.baseMapper.selectPage(page, queryWrapper);
        WxMiniPageDto<MyOrderDto> result = new WxMiniPageDto(page.getCurrent(), page.getSize());
        BeanUtils.copyProperties(page, result);
        List<MyOrderDto> convertList = page.getRecords().stream().map(po -> {
            MyOrderDto dto = new MyOrderDto();
            BeanUtils.copyProperties(po, dto);
            return dto;
        }).collect(Collectors.toList());
        result.setData(convertList);
        return result;
    }

    @Override
    public List<MyOrder> selectList(MyOrderQueryParam queryParam) {
        QueryWrapper<MyOrder> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(queryParam)) {
            if (!ObjectUtils.isEmpty(queryParam.getStaffid())) {
                queryWrapper.eq("staffid", queryParam.getStaffid());
            }
            if (!ObjectUtils.isEmpty(queryParam.getShopid())) {
                queryWrapper.eq("shopid", queryParam.getShopid());
            }
            if (!ObjectUtils.isEmpty(queryParam.getUtype())) {
                queryWrapper.eq("utype", queryParam.getUtype());
            }
            if (!ObjectUtils.isEmpty(queryParam.getType())) {
                queryWrapper.eq("type", queryParam.getType());
            }
            if (!ObjectUtils.isEmpty(queryParam.getOrderno())) {
                queryWrapper.eq("orderno", queryParam.getOrderno());
            }
            if (!ObjectUtils.isEmpty(queryParam.getSourceType())) {
                queryWrapper.eq("source_type", queryParam.getSourceType());
            }
        } else {
            return null;
        }
        queryWrapper.orderByDesc("addtime");
        List<MyOrder> result = this.baseMapper.selectList(queryWrapper);
        return result;
    }

    /**
     * 获取工厂会员总数；
     */
    @Override
    public Long selectCount(MyOrderQueryParam queryParam) {
        QueryWrapper<MyOrder> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(queryParam)) {
            if (!ObjectUtils.isEmpty(queryParam.getStaffid())) {
                queryWrapper.eq("staffid", queryParam.getStaffid());
            }
            if (!ObjectUtils.isEmpty(queryParam.getShopid())) {
                queryWrapper.eq("shopid", queryParam.getShopid());
            }
            if (!ObjectUtils.isEmpty(queryParam.getUtype())) {
                queryWrapper.eq("utype", queryParam.getUtype());
            }
            if (!ObjectUtils.isEmpty(queryParam.getType())) {
                queryWrapper.eq("type", queryParam.getType());
            }
            if (!ObjectUtils.isEmpty(queryParam.getOrderno())) {
                queryWrapper.eq("orderno", queryParam.getOrderno());
            }
            if (!ObjectUtils.isEmpty(queryParam.getSourceType())) {
                queryWrapper.eq("source_type", queryParam.getSourceType());
            }
        } else {
            return 0L;
        }
        Long result = this.baseMapper.selectCount(queryWrapper);
        return result;
    }

    /**
     * 根据Id获取工厂会员列表；
     */
    @Override
    public MyOrder selectById(Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            return null;
        }
        MyOrder myShopMember = this.baseMapper.selectById(id);
        return myShopMember;
    }

    /**
     * 新增工厂会员；
     */
    @Override
    public MyOrder addEntity(MyOrder entity) {
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (!ObjectUtils.isEmpty(entity.getId())) {
            return null;
        }
        this.baseMapper.insert(entity);
        return entity;
    }

    /**
     * 更新工厂会员
     */
    @Override
    public MyOrder updateEntity(MyOrder entity) {
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (!ObjectUtils.isEmpty(entity.getId())) {
            return null;
        }
        this.baseMapper.updateById(entity);
        return entity;
    }

    @Override
    public Boolean deleteById(Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            return false;
        }
        return this.baseMapper.deleteById(id) > 0 ? true : false;
    }

    @Override
    public Boolean deleteStaffAndType(Integer staffId, Integer type) {
        if (ObjectUtils.isEmpty(staffId) || ObjectUtils.isEmpty(type)) {
            return false;
        }
        QueryWrapper<MyOrder> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(staffId)) {
            queryWrapper.eq("staffid", staffId);
        }
        if (!ObjectUtils.isEmpty(type)) {
            queryWrapper.eq("type", type);
        }
        return this.baseMapper.delete(queryWrapper) > 0 ? true : false;
    }

    @Override
    public Boolean deleteStaffAndId(Integer staffId, Integer id) {
        if (ObjectUtils.isEmpty(staffId) || ObjectUtils.isEmpty(id)) {
            return false;
        }
        QueryWrapper<MyOrder> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(staffId)) {
            queryWrapper.eq("staffid", staffId);
        }
        if (!ObjectUtils.isEmpty(id)) {
            queryWrapper.eq("id", id);
        }
        return this.baseMapper.delete(queryWrapper) > 0 ? true : false;
    }
}
